package com.mlm.mlmapp.adapter.view;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.androidquery.AQuery;
import com.androidquery.util.Constants;
import com.mlm.mlmapp.R;
import com.mlm.mlmapp.adapter.view.listener.OnBannerClickedListener;
import com.mlm.mlmapp.cons.HTTPCons;
import com.mlm.mlmapp.utils.CommonUtils;

@SuppressLint("ValidFragment")
public class BannerFragment extends Fragment  {
	AQuery aq;
	private Context context;
	private String acturl;
	private OnBannerClickedListener listener;
	public BannerFragment(Context con,OnBannerClickedListener listener) {

		this.context = con;
		this.listener=listener;

	}

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.banner_view, container, false);
		ImageView iv = (ImageView) view.findViewById(R.id.iv_banner);
		if (aq != null) {
			aq = aq.recycle(view);
		}
		aq = new AQuery(view);

		Bundle b = getArguments();

		try {
			JSONObject json = new JSONObject(b.getString(HTTPCons.JSON));
			
			final String imageBanner = CommonUtils.getJsonString(json, HTTPCons.IMG_BANNER_URL);
			final String actBanner = CommonUtils.getJsonString(json, HTTPCons.ACT_BANNER_URL);
			aq.id(R.id.iv_banner).image(imageBanner, true, true, 400, 0, null,
					Constants.FADE_IN);
			this.acturl=actBanner;
			iv.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					listener.onItemClick(arg0,actBanner );
				}
			});
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return view;
	}

	public String getActurl() {
		return acturl;
	}



}
